<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            let obj = {
                msg: 'hello',
                user: {
                    name: 'tom',
                    home: {
                        address: 'china',
                    },
                },
                list: [
                    {
                        id: 55,
                        age: 66,
                    },
                ],
            };
            function deal(data, str) {
                str = str.replace(/\[/, '.'); //list.0].id
                str = str.replace(/\]/, ''); //list.0.id
                console.log(str);
                let [key, ...params] = [...str.split('.')];
                console.log(key, params);
                console.log('typeof -- ', typeof data[key], data[key] instanceof Object);

                if (data[key] instanceof Object) {
                    return deal(data[key], params.join('.'));
                } else {
                    return data[key];
                }
            }

            console.log('msg  ==> ', deal(obj, 'msg'));
            console.log('user.name  ==> ', deal(obj, 'user.name'));
            console.log('user.home.address  ==> ', deal(obj, 'user.home.address'));
            console.log('list[0].id  ==> ', deal(obj, 'list[0].id'));
        </script>
    </body>
</html>
